package com.olt.controller.login;

import com.olt.domain.user.OltUser;
import com.olt.result.ErrorEnum;
import com.olt.result.Result;
import com.olt.server.shiro.dto.ShiroLoginDto;
import com.olt.server.shiro.dto.ShiroLoginOutput;
import com.olt.server.user.UserServer;
import com.olt.server.user.dto.GetUserForEdit;
import com.olt.server.user.dto.RegitInput;
import com.olt.server.user.dto.SendEmailInput;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import util.*;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;


@RestController
public class LoginController {
    @Autowired
    UserServer userServer;


    @PostMapping(value = "api/login")
    public Result<ShiroLoginOutput> login (@RequestBody ShiroLoginDto input){

        UsernamePasswordToken token = new UsernamePasswordToken(input.userName,input.passWord);
        Result<ShiroLoginOutput> res=new Result<>();
        ShiroLoginOutput out=new ShiroLoginOutput();
        try {
        SecurityUtils.getSubject().login(token);
        Subject subject = SecurityUtils.getSubject();
        OltUser user=(OltUser) subject.getPrincipal();
        var custom=userServer.findOltCustom(user.getUserId());
        out.setName(custom.getName());
        out.setUuid(custom.getCustomId());
        Serializable tokenId = subject.getSession().getId();
        out.setToken(String.valueOf(tokenId));
        var role=userServer.getRoleByUserId(user.getUserId());
        var per=userServer.getPowerByRole(role.getRoleId());
        out.setRoleDto(role);
        out.setPermissionDto(per);
        out.setPagePermissions(PageUtil.getPage(per.getPermission(),role.getRoleName()));


        } catch (Exception e) {
            res.setError(ErrorEnum.ERROR_Login);
            return res;
        }
        res.setError(ErrorEnum.Success_Http);
        res.setValue(out);
        return res;


    }

    @GetMapping("pages/getPages")
    public  Result<ShiroLoginOutput> getPages(){
        Subject subject = SecurityUtils.getSubject();
        OltUser user=(OltUser) subject.getPrincipal();
        ShiroLoginOutput out=new ShiroLoginOutput();
        Result<ShiroLoginOutput> r= new Result<>();
        try {
            var role=userServer.getRoleByUserId(user.getUserId());
            var per=userServer.getPowerByRole(role.getRoleId());
            out.setPagePermissions(PageUtil.getPage(per.getPermission(),role.getRoleName()));
            out.setRoleDto(role);
            out.setPermissionDto(per);
            r.setError(ErrorEnum.Success_Http);
            r.setValue(out);
            return r;

        } catch (Exception exception) {
            exception.printStackTrace();
            r.setError(ErrorEnum.NOT_LOGIN);
            return r;
        }
    }

    @PostMapping("getVerifyCode")
    public Result<Boolean> getVerifyCode(@RequestBody SendEmailInput input){
        var res=new Result<Boolean>();
        var temp= userServer.sendEmail(input);
        res.setValue(temp);
        if(temp){
            res.setError(ErrorEnum.Success_Http);
        }
        else {
            res.setError(ErrorEnum.MyError("发送失败"));
        }
        return  res;

    }
    @PostMapping("register")
    public Result<Boolean> register(@RequestBody RegitInput input){
        var res=new Result<Boolean>();
        var temp= userServer.register(input);
        res.setValue(temp);
        if(temp){
            res.setError(ErrorEnum.Success_Http);
        }
        else {
            res.setError(ErrorEnum.MyError("验证码无效"));
        }
        return  res;

    }

    @PostMapping("uploadAvatar")
    public Result<String> uploadAvatar( @RequestParam("uuid")String id,@RequestParam("file")MultipartFile file)throws IOException {
        String path= ResourcePath.getResInstance().AvatarResource;
        File f= OltFileUtil.SaveFile(file);
        String fileName=id+".png";
        File dst=new File( path+File.separator+fileName);
        if(!dst.exists()){
            dst.createNewFile();
        }
        ResizeImage.imageConvertCommon(f,dst,"png");
        f.delete();
        var res=new Result<String>();
        res.setValue(fileName);
        res.setError(ErrorEnum.Success_Http);
        return  res;

    }
    @GetMapping("api/getUserMessage")
    public Result<GetUserForEdit> getUserMessage(@RequestParam("uuid") String id) {
        var userMessage=userServer.findUserForeEdit(id);
        var res=new Result<GetUserForEdit>();
        res.setValue(userMessage);
        res.setError(ErrorEnum.Success_Http);
        return  res;

    }

    @PostMapping("api/editUser")
    public Result<Boolean> getUserMessage(@RequestBody GetUserForEdit input) {
        var userMessage=userServer.editUser(input);
        var res=new Result<Boolean>();
        res.setValue(userMessage);
        res.setError(ErrorEnum.Success_Http);
        return  res;

    }



}
